home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: aril@cmt.lpr.mail.carel.fi (Ari Lukumies)
- Newsgroups: comp.lang.c
- Subject: Re: QUESTION about pointers and structures
- Date: Thu, 11 Jan 1996 12:58:25 GMT
- Organization: Carelcomp Forest Oy
- Message-ID: <4d31u1$kdv@tahko.lpr.carel.fi>
- References: <4d1rqg$bse@mirv.unsw.edu.au>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- X-Newsreader: Forte Free Agent 1.0.82
-
- gwong@cse.unsw.edu.au (Geoffrey Wong) wrote:
-
- >Hi guys,
- > I am wondering if anyone of you c legends could help me. I have a
- > rather complex data structure.
- > It is a pointer to structure which contains pointer to structures.
- > I don't know if I need to allocate space for variables of this complex
- > type before I can do the assignments.
- >
- > I have a variable called background.
- > declared as:-
- > SLiteral *background;
- >
- > Now SLiteral is:-
- >
- > typedef struct _slit_rec *SLiteral, /* Used in rlgg */
- >
- > and _slit_rec is:-
- >
- > struct _slit_rec
- >{
- > char Sign; /* 0=negated, 1=pos, 2=determinate */
- > Relation Rel;
- > Const *Constants; /* During an rlgg process Constant and Var is
- > coded using the same data type. This is
- > possible since Const is larger than Var! */
- > Which *ConstVar; /* 0=const, 1=Var for each Constants */
- > int WeakLits; /* value up to this literal */
- > Ordering *ArgOrders, /* recursive lits: =, <, >, # */
- > *SaveArgOrders; /* copy during pruning */
- > float Bits; /* encoding length */
- >};
- >Now my question is can I just say background[i]->Sign = ???? or do I need to
- >allocate space for background[i] or do I need to allocate space for *background
- >Any help would be appreciated, I am rather new to c, don't know much tricks
- >yet.
-
- >geoff.
-
- I take it you want to have a variable background, which is a pointer
- to many struct _slit_rec's? If so, you will do either
-
- typedef struct _slit_rec *SLiteral;
- SLiteral background;
-
- or
- typedef struct _slit_rec SLiteral;
- SLiteral *background;
-
- I'd prefer the latter, because the declaration of background clearly
- shows that it's to be a pointer, so in the examples below I've taken
- that approach.
-
- In your code, you'll end up actually doing
-
- struct _slit_rec **background;
-
- because SLiteral is already a pointer type (hence the *), and you
- define background as a pointer of this (pointer) type. If you want to
- have, say, 100 background objects, you can allocate them all at once:
-
- background = (SLiteral *)calloc(100, sizeof(SLiteral));
-
- and then you can use
-
- for (i = 0; i < 100; i++)
- something = background[i].Sign;
-
- Remember to free(background) when you're done.
-
- If you still (for some unknown reason) wanted to use your original
- declaration (**background), you would have to (Note: here SLiteral is
- a pointer type to struct _slit_rec):
-
- background = (SLiteral*)calloc(100, sizeof(SLiteral));
- for (i = 0; i < 100; i++)
- background[i] = (SLiteral)calloc(1, sizeof(struct _slit_rec));
-
- You must use sizeof(struct _slit_rec) here, because the
- sizeof(SLiteral) is only the size of the pointer (usually 4 bytes).
-
- As myself, I usually use structure definitions such as:
-
- typedef struct a_struct {
- .....
- } A_STRUCT, *PA_STRUCT;
-
- which will give me a) typedef for the structure itself (A_STRUCT) and
- a typedef for a pointer to is (PA_STRUCT). Thus, I can write:
-
- PA_STRUCT background; /* P tells me it's a pointer type */
-
- background = (PA_STRUCT)calloc(100, sizeof(A_STRUCT));
-
- HTH,
-
- AriL
-
- All my opinions are mine and mine alone.
-
-